package com.yy.cloud.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * 全局对跨域进行处理的配置类
 *
 * @date 2024/5/11
 */
@Configuration
public class CorsConfig {

    @Bean
    public CorsFilter CorsFilter() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedMethod("*"); // 允许的请求方式：get、post、..，*号代表所有方式都可以
        corsConfiguration.addAllowedOrigin("*"); // 允许的请求来源，例如：http://www.baidu.com ...*号代表所有来源都可以
        corsConfiguration.addAllowedHeader("*"); // 允许的请求头

        // 允许访问的资源
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", corsConfiguration);

        return new CorsFilter(source);
    }
}
